package com.bjsxt.reflex;

import java.lang.reflect.Method;
//反射机制的效率对比
public class TestTime {
    public static void main(String[] args) throws Exception {
        Class clazz= Class.forName("com.bjsxt.reflex.Users");
        Users u=(Users)clazz.newInstance();
        long start = System.currentTimeMillis();
        Method m =clazz.getMethod("setUsername",String.class);
        m.setAccessible(true);// 关闭安全检可以提高效率
        for (int i=0;i<10000000;i++){
            m.invoke(u,"oldlu");
        }
        long end = System.currentTimeMillis();
        System.out.println("反射耗时："+(end-start));
        long start2= System.currentTimeMillis();
        Users u1  = new Users();
        for (int i=0;i<10000000;i++){
            u1.setUsername("oldlu");
        }
        long end2 = System.currentTimeMillis();
        System.out.println("非反射耗时："+(end2-start2));

    }
}
